package m.jclecai.com.adapter;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.LayoutRes;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.base.ViewHolerAdapter;
import m.jclecai.com.bean.Response.ResFollowOrederPeopleList;
import m.jclecai.com.adapter.base.ViewHolerAdapter;
import m.jclecai.com.adapter.holder.ViewHolder;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class OrderFollowPeopleAdapter extends ViewHolerAdapter<FollowPeopleViewHolder> {


    private Context context;
    private List<ResFollowOrederPeopleList.FollowPeopleBean> arrayFollowPeople;

    public OrderFollowPeopleAdapter(Context context, List<ResFollowOrederPeopleList.FollowPeopleBean> arrayFollowPeople) {
        this.context = context;
        this.arrayFollowPeople = arrayFollowPeople;
    }


    @Override
    public FollowPeopleViewHolder onCreateViewHolder(int position, ViewGroup parent) {
        return new FollowPeopleViewHolder(context, R.layout.item_fragment_follow_people);
    }

    @Override
    public void onBindViewHolder(int position, FollowPeopleViewHolder followPeopleViewHolder) {
        followPeopleViewHolder.init(arrayFollowPeople.get(position), position);
    }

    @Override
    public int getCount() {
        return arrayFollowPeople == null ? 0 : arrayFollowPeople.size();
    }

    public void refersh(List<ResFollowOrederPeopleList.FollowPeopleBean> datas) {
        arrayFollowPeople.clear();
        arrayFollowPeople.addAll(datas);
        notifyDataSetChanged();
    }

    public void addAll(List<ResFollowOrederPeopleList.FollowPeopleBean> datas) {
        arrayFollowPeople.addAll(datas);
        notifyDataSetChanged();
    }

    public void clear() {
        arrayFollowPeople.clear();
        notifyDataSetChanged();
    }
}

class FollowPeopleViewHolder extends ViewHolder {

    @BindView(R.id.tv_username)
    TextView mTvUsername;

    @BindView(R.id.tv_one_money)
    TextView mTvOneMoney;

    @BindView(R.id.tv_add_time)
    TextView mTvAddTime;

    @BindView(R.id.ll_item)
    LinearLayout mLlItem;

    public FollowPeopleViewHolder(Context context, @LayoutRes int resource) {
        super(context, resource);
    }

    public void init(ResFollowOrederPeopleList.FollowPeopleBean followPeopleBean, int position) {
        mTvUsername.setText(followPeopleBean.getUsername());
        mTvOneMoney.setText(followPeopleBean.getOne_money());


        String date = "";
        if (followPeopleBean.getAddtime() != null) {


            try {
                Date parse = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(followPeopleBean.getAddtime());
                date = new SimpleDateFormat("MM-dd hh:mm").format(parse);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            if (TextUtils.isEmpty(date)) {
                date = followPeopleBean.getAddtime();
            }

            mTvAddTime.setText(date);
            mLlItem.setBackground(new ColorDrawable(getResources().getColor(R.color.color_f5f5f5)));
        }
    }
}

